package cn.tedu.pet.product.controller;


import cn.tedu.pet.commons.pojo.vo.PageData;
import cn.tedu.pet.product.pojo.param.ClientAddNewParam;
import cn.tedu.pet.product.pojo.param.ClientUpdateInfoParam;
import cn.tedu.pet.product.pojo.vo.ClientIndexVO;
import cn.tedu.pet.product.pojo.vo.ClientListItemVO;
import cn.tedu.pet.product.pojo.vo.ClientStandardVO;
import cn.tedu.pet.product.pojo.vo.PetStandardVO;
import cn.tedu.pet.product.service.IClientService;
import cn.tedu.pet.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RequestMapping("/client")
@RestController
@Api(tags = "客户管理模块")
@Slf4j
@Validated
public class ClientController {
    @Autowired
    private IClientService clientService;

    // http://localhost:9080/client/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加客户")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid ClientAddNewParam clientAddNewParam){
        log.debug("开始处理【添加客户】的请求，参数：{}",clientAddNewParam);
        clientService.addNew(clientAddNewParam);
        log.debug("处理【添加客户】的请求，完成！");
        return JsonResult.ok();
    }

    // http://localhost:9080/client/delete
    @PostMapping("/delete")
    @ApiOperation("删除客户")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "客户ID", required = true, dataType = "long")
    })
    public JsonResult delete(@Range(min = 1,message = "根据ID删除相册失败，请提交合法的ID值！")
                             @RequestParam Long id){
        log.debug("开始处理【删除客户】的请求，参数：{}",id);
        clientService.deleteById(id);
        log.debug("处理【删除客户】的请求，完成！");
        return JsonResult.ok();
    }

    // http://localhost:9080/client/update
    @PostMapping("/update")
    @ApiOperation("修改客户")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "客户ID", required = true, dataType = "long")
    })
    public JsonResult update(@Range(min = 1,message = "根据ID修改相册失败，请提交合法的ID值！")
                             @RequestParam Long id,
                             @Valid ClientUpdateInfoParam clientUpdateInfoParam){
        log.debug("开始处理【修改客户】的请求，id:{}，参数：{}",id,clientUpdateInfoParam);
        clientService.updateById(id,clientUpdateInfoParam);
        log.debug("处理【修改客户】的请求，完成！");
        return JsonResult.ok();
    }

    // http://localhost:9080/client/list
    @GetMapping("/list")
    @ApiOperation("查询客户")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码",paramType = "query")
    })
    public JsonResult list(@Range(min = 1,message = "查询客户列表失败，请提供正确的页码值！")Integer page){
        log.debug("开始处理【查询客户列表】的请求，参数：{}",page);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<ClientListItemVO> pageData = clientService.list(page);
        return JsonResult.ok(pageData);
    }

    // http://localhost:9080/client/standard
    @GetMapping("/standard")
    @ApiOperation("根据ID查询客户详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "客户ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1, message = "根据ID查询客户详情失败，请提交合法的ID值！")
                                      @RequestParam Long id) {
        log.debug("开始处理【根据ID查询客户详情】的请求，参数：{}", id);
        ClientStandardVO result = clientService.getStandardById(id);
        return JsonResult.ok(result);
    }

    // http://localhost:9080/client/search
    @GetMapping("/search")
    @ApiOperation("模糊查询客户详情")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wd", value = "查询信息", required = true)
    })
    public JsonResult search(String wd) {
        log.debug("开始处理【模糊查询客户详情】的请求，参数：{}", wd);
        List<ClientIndexVO> client = clientService.search(wd);
        return JsonResult.ok(client);
    }


    // http://localhost:9080/client/setVip
    @PostMapping("/setVip")
    @ApiOperation("启用类别")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult setVip(@RequestParam @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.debug("开始处理【启用类别】的请求，参数：{}", id);
        clientService.setVip(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/client/disable
    @PostMapping("/disable")
    @ApiOperation("禁用类别")
    @ApiOperationSupport(order = 440)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult setDisable(@RequestParam @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.debug("开始处理【禁用类别】的请求，参数：{}", id);
        clientService.setDisable(id);
        return JsonResult.ok();
    }

    @GetMapping("/standardbypetname")
    @ApiOperation("根据name查询宠物详情")
    @ApiOperationSupport(order = 450)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "宠物名字", example = "all")
    })
    public JsonResult getStandardById(@RequestParam String name) {
        log.debug("开始处理【根据ID查询宠物详情】的请求，参数：{}", name);
        PetStandardVO result = clientService.getStandardByName(name);
        return JsonResult.ok(result);
    }


}
